package com.baihan.angus.app.game.busshow.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.ScriptResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;

public class ScriptLibraryAdp extends BaseAdp<ScriptResponse.DataX.Data, ScriptLibraryAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_script_library, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        ScriptResponse.DataX.Data scriptResponse = getItem(position);

        vh.tvName.setText(scriptResponse.getName());
        vh.tvPeople.setText(scriptResponse.getNum() + "人");

        if (null != scriptResponse.getScript_type_text()) {
            int size = scriptResponse.getScript_type_text().size();
            if (size == 1) {
                vh.tvType2.setVisibility(View.GONE);
                vh.tvType1.setText(scriptResponse.getScript_type_text().get(0));
            } else if (size == 2) {
                vh.tvType2.setVisibility(View.VISIBLE);
                vh.tvType1.setText(scriptResponse.getScript_type_text().get(0));
                vh.tvType2.setText(scriptResponse.getScript_type_text().get(1));
            }
        }

        int difficulty = scriptResponse.getDifficulty();
        if (difficulty == 0) {//0简单 1一般 2困难 3极难
            vh.tvDifficulty.setText("简单");
        } else if (difficulty == 1) {
            vh.tvDifficulty.setText("一般");
        } else if (difficulty == 2) {
            vh.tvDifficulty.setText("困难");
        } else if (difficulty == 3) {
            vh.tvDifficulty.setText("极难");
        }
        vh.tvIntro.setText(scriptResponse.getIntro());
        vh.tvScore.setText(scriptResponse.getScore());

        ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                vh.ivPic, scriptResponse.getCover(), R.mipmap.ic_test_bg, 4);

        vh.llScriptItem.setOnClickListener(v -> {
            mOnItemClickListener.onItemClick(position);
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivPic;
        TextView tvName, tvScore, tvPeople, tvType1, tvType2, tvDifficulty, tvIntro;
        LinearLayout llScriptItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.ivPic);
            tvName = itemView.findViewById(R.id.tvName);
            tvPeople = itemView.findViewById(R.id.tvPeople);
            tvType1 = itemView.findViewById(R.id.tvType1);
            tvType2 = itemView.findViewById(R.id.tvType2);
            tvDifficulty = itemView.findViewById(R.id.tvDifficulty);
            tvIntro = itemView.findViewById(R.id.tvIntro);
            tvScore = itemView.findViewById(R.id.tvScore);
            llScriptItem = itemView.findViewById(R.id.llScriptItem);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
